// pages/searchOrder/searchOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    accountFocus: true,
    accountSearchHistory: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    options.pageName && (wx.setNavigationBarTitle({
      title: options.pageName,
    }))
  },

  // 账号输入框监听
  bindAccountInput: function(e) {
    this.setData({
      account: e.detail.value
    })
  },

  // 点击历史记录
  historyClick: function(e) {
    console.log(e)
    this.data.account = e.currentTarget.dataset.value;
    this.bindSearchOrder();
  },

  // 执行查询
  bindSearchOrder: function() {
    var that =this;
    if(this.data.account!=''&&this.data.account!=null){
      var AccountSearchHistory = wx.getStorageSync('AccountSearchHistory') || [];
      var isAddHistory = true;
      for (var i = 0; i < AccountSearchHistory.length; i++) {
        if (AccountSearchHistory[i] === this.data.account) {
          isAddHistory = false;
          break;
        }
      }
      if (isAddHistory) {
        if (AccountSearchHistory.length >= 6) {
          AccountSearchHistory.splice(0, 1)
        }
        AccountSearchHistory.push(this.data.account)
        wx.setStorage({
          key: 'AccountSearchHistory',
          data: AccountSearchHistory,
        })
      }
      wx.navigateTo({
        url: '/pages/agent/plugins/tencentCloud/searchOrderList/searchOrderList?account=' + that.data.account,
      })
    }else{
      app.showConfirm('提示', '请输入搜索的客户账号ID', false)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let history = wx.getStorageSync('AccountSearchHistory') || [];
    this.setData({
      accountSearchHistory: history.reverse()
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    app.pageOnloadInit(this, app);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
